<?php
/**
 * Data Class
 * 
 * This class is here to load other database classes.
 * 
 * Ideas for the layout received from simon_w on the bit-tech forums.
 * 
 * @package shoehorn
 * @subpackage data
 * 
 */

abstract class Shoehorn_Data  // filename = ./data.class.php
{
	protected $dsn = '';  //protected so extensions can access it
	protected $err = ''; //protected so extensions can access it
	
	function __construct($dsn, &$err)
	{
		$this->dsn = $dsn;
		$this->err = $err;
	}
	
	/**
	 * Execute a query
	 *
	 * @param string $query
	 * @return resource|false;
	 */
	abstract function execute($query);
	
	/**
	 * retrieve next row as an array
	 * 
	 * @return array|false;
	 */
	abstract function nextRow($resultIdentifier);
	
	// mysql/user:pass,database@host
	
	/**
	 * Get an Instance of the Data class
	 * 
	 * This function simply determines which class needs to be loaded, then 
	 * loads it as an extension of this class.  It also requires the class file
	 * if needed.
	 *
	 * @param string $dsn
	 * @return object|false
	 */
	static function getInstance($dsn, &$err)
	{
		//get the driver from the DSN string
		$driver = substr($dsn, 0, strpos($dsn, '/'));
		
		$driverClass = __CLASS__ . '_' . ucfirst($driver);
		if(!class_exists($driverClass)) {
			if(!require_once(SHOEHORN_SCRIPT_FOLDER . '/'
			 . strtolower($driverClass) . '.class.php')) {
				return false;
			 }
		}
		//obviously if we got this far, nothing went wrong.
		return new $driverClass($dsn, $err);
	}
}